package org.languageguide.readings;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Definition {
	Integer occurrence;
	String matchingText;
	String header;
	String body;
	String exampleSentence;
	String exampleSentenceTranslation;
	String type;
	Integer id;
	Integer order;
	private String defBody;
	private String translatedBody;
	
	public Definition(ResultSet set) throws SQLException{//(String matchingText, Integer occurrence, String header, String  body, String exampleSentence){
		this.id = set.getInt("Id");
		this.order = set.getInt("SortingOrder");
		this.matchingText = set.getString("MatchingText");
		this.type = set.getString("Type");
		this.occurrence = set.getInt("Occurrence");
		this.header = set.getString("Header");
		this.defBody = set.getString("ReadingDefinitions.Body");
		this.translatedBody = set.getString("ReadingDefinitionTranslation.Body");
		this.body = set.getString("Body");
		this.exampleSentence = set.getString("ExampleSentence");
		this.exampleSentenceTranslation = set.getString("ExampleSentenceTranslation");
	}
	
	
	public Definition(Integer id, Integer sortingOrder, String translatedBody){
		this.id= id;
		this.order = sortingOrder;
		this.translatedBody = translatedBody;
	}

	public boolean getFullDefinition(){
		if (translatedBody != null || defBody != null){
			if (translatedBody.equals("") && defBody == null)
				return false;
			return true;
		}
		return false;
	}
	
	public boolean hasBody(){
		if (translatedBody != null || defBody != null){
			if (translatedBody.equals("") && defBody == null)
				return false;
			return true;
		}
		return false;
	}
	
	public String getKey(){
		if (order == 0)
			return "" + id;
		else return "" + id + "-" + order; 
	}
	
	public String getMatchingText(){
		return matchingText;
	}
	
	public Integer getOccurrence(){
		return occurrence;
	}
	
	public String generateDefLink(String textToWrap){
		return "<span class=\"linked\" id=\"" + id + "\">" + textToWrap + "</span>";
	}
	
	public String generateDefLink(){
		return "<span class=\"linked\" id=\"" + id + "\">" + matchingText + "</span>";
	}
	
	public String getHeader(){
		if (header != null){
			return header;
		}
		else return matchingText;
	}
	
	public String getBodyDiv(){ 
		return "<div id=\"d" + id + "\" class=\"pop_up\">" + getFullDefintion() + "</div>";
	}
	
	public String getType(){
		if (type == null)
			return "";
		return type;
	}
	
	public String getBody(){
		if (defBody != null){
			if (translatedBody != null){
				return defBody + "<br />" + translatedBody;
			}
			else return defBody;
		}
		else{
			if (translatedBody != null)
				return translatedBody;
			else return "";
		}
	}
	
	public String getFullDefintion(){
		StringBuilder builder = new StringBuilder();
		if (header != null){
			builder.append("<strong>" + header  + "</strong><br />");
		}
		if (defBody != null){
			if (translatedBody != null){
				builder.append(defBody + "<br />" + translatedBody);
			}
			else builder.append(defBody);
		}
		else{
			if (translatedBody != null)
				builder.append(translatedBody);
			else return builder.toString();
		}
		return builder.toString();
	}
	
	public String getTranslationBody(){
		if (translatedBody != null)
			return translatedBody;
		else return "";
	}
	
	public String getTranslationBodyPure(){
		return translatedBody;
	}
	
	public String getExampleSentence(){
		return exampleSentence;	
	}
	
	public String getExampleSentenceTranslation(){
		return exampleSentence;
	}
	
	public Integer getId(){
		return id;
	}
	
	public Integer getOrder(){
		return order;
	}
	
}